<script>
import {addOrUpdateUser, getUserList, removeOneUser, removeUsers, userPage} from "@/api/user";

export default {
  name: "index",
  data() {
    return {
      currentPage: 1,
      size: 5,
      tableData: [],
      dialogFormVisible: false,
      formLabelWidth: '80px',
      form: {
        id: '',
        username: '',
        nickname: '',
        age: '',
        email: ''
      },
      title: '新增',
      total: 0,
      selectionIds:''
    }
  },
  // 定义事件回调函数
  methods: {
    // 编辑数据回调函数
    handleEdit(index, row) {
      console.log(index, JSON.stringify(row));
      this.title = '编辑'
      this.form = row
      // 显示模态窗
      this.dialogFormVisible = true
    },
    // 删除数据回调函数
    handleDelete(index, row) {
      // 确认提示
      this.$confirm('此操作将永久删除行数据, 是否继续?', '警告', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 发送 http请求    删除一条数据
        removeOneUser(row.id).then(response=>{
          if(response.code==200){
            this.$message.success(response.message)
            // 重载数据
            this.initUserPage()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });



    },
    // 复选框回调函数
    handleSelectionChange(val) {
      let result = val.map(v=>v.id)
      this.selectionIds = result.join(',')
    },
    // 监听每页显示数据条数的改变
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.size = val
      this.initUserPage()
    },
    // 监听页码的改变
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val
      // 再次发送分页请求
      this.initUserPage()
    },
    onSubmit() {
      // 发送http请求    添加用户信息
      console.log(JSON.stringify(this.form))
      addOrUpdateUser(this.form).then(response => {
        if (response.code == 200) {
          this.$message.success(response.message)
          // 关闭弹窗
          this.dialogFormVisible = false
          // 表格数据重载
          this.initUserPage()
        } else {
          this.$message.error(response.message)
        }
      })
    },
    clearInfo() {
      this.title = '新增'
      // 清空表单数据
      this.form = {
        id: '',
        username: '',
        nickname: '',
        age: '',
        email: ''
      }
    },
    initUserPage() {
      // 实现分页
      userPage(this.currentPage, this.size).then(response => {
        if (response.code == 200) {
          this.tableData = response.data
          this.total = response.count
        }
      })
    },
    deleteUsers(){
        if (this.selectionIds.length==0){   // 判定用户是否选择了行数据
            this.$message.warning('请至少选择一条数据')
            return  false
        }
        // 发送http请求 删除选中行数据
        removeUsers(this.selectionIds).then(response=>{
            if (response.code==200){
               this.$message.success(response.message)
               //重载数据
               this.initUserPage()
            }else {
              this.$message.error(response.message)
            }
        })
    }
  },
  created() {
    // 发送http请求  axios 异步请求库   用户列表  promise 对象  /user/list
    this.initUserPage()
  }
}
</script>

<template>
  <div id="container">
    <div id="btn-header">
      <el-button type="primary" plain size="small" @click="dialogFormVisible=true">新增</el-button>
      <el-button type="success" plain size="small" @click="deleteUsers">批量删除</el-button>
    </div>
    <!-- selection-change  检测复选框的选中状态       -->
    <el-table
        @selection-change="handleSelectionChange"
        :data="tableData"
        stripe
        style="width: 100%">
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column
          prop="username"
          label="用户名"
      >
      </el-table-column>
      <el-table-column
          prop="nickname"
          label="昵称"
      >
      </el-table-column>
      <el-table-column
          prop="age"
          label="年龄"
      >
      </el-table-column>
      <el-table-column
          prop="email"
          label="邮箱">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)">编辑
          </el-button>
          <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div id="pagination">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="5"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
    <div id="dialog">
      <el-dialog :title="title" :visible.sync="dialogFormVisible" @close="clearInfo">
        <el-form :model="form">
          <el-input type="hidden" v-model="form.id" autocomplete="off"></el-input>
          <el-form-item label="用户名" :label-width="formLabelWidth">
            <el-input v-model="form.username" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="昵称" :label-width="formLabelWidth">
            <el-input v-model="form.nickname" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="年龄" :label-width="formLabelWidth">
            <el-input v-model="form.age" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" :label-width="formLabelWidth">
            <el-input v-model="form.email" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="onSubmit">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<style scoped>
#btn-header {
  text-align: left;
  margin-top: 15px;
  margin-bottom: 15px;
}

#container {
  padding: 20px;
}

#pagination {
  margin-top: 15px;
}

#dialog {
  text-align: left;
}
</style>